// pages/grzl/grzl.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        array: ["大一", "大二", "大三", "大四", "研究生", "已毕业"],
        array1: ["计算机学院", "化学学院", "物理学院", "数学学院", "电气学院", "土木学院", "生科学院"],
        College_index: 0,
        Grade_index: 0,
        openid: '',
        cloudImgList: [],
    },
    //获取个人资料
    getPersonalData() {
        let user = wx.getStorageSync('user')
        this.setData({ openid: user._openid })
        db.collection("user").where({ _openid: this.data.openid }).get().then(res => {
            this.setData({
                userInfo: res.data
            })
            console.log(this.data.userInfo[0].Grade)
            for (let i = 0; i < this.data.array.length; i++) {
                if (this, this.data.array[i] == this.data.userInfo[0].nj) {
                    console.log(i)
                    this.setData({
                        Grade_index: i
                    })
                }
            }
            for (let i = 0; i < this.data.array1.length; i++) {
                if (this, this.data.array1[i] == this.data.userInfo[0].College) {
                    console.log(i)
                    this.setData({
                        College_index: i
                    })
                }
            }
        })
    },
    //选择照片
    chooseImage() {
        let that = this
        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths);
                that.setData({
                    cloudImgList: tempFilePaths
                })
            }
        })
    },
    //获取年级
    bindchangeGrade: function (e) {
        console.log(e.detail.value)
        this.setData({
            Grade_index: e.detail.value
        })
    },
    //获取学院
    bindchangeCollege: function (e) {
        console.log(e.detail.value)
        this.setData({
            College_index: e.detail.value
        })
    },
    //提交修改的数据
    formSubmit(e) {
        console.log(e.detail.value)
        db.collection("user").where({ _openid: this.data.openid }).update({
            data: {
                Grade: this.data.array[this.data.Grade_index],
                phone: e.detail.value.phone,
                nickName: e.detail.value.name,
                College: this.data.array1[this.data.College_index],
                studentid: e.detail.value.xh,
                _updateTime: Date.parse(new Date()),
                profile: this.data.cloudImgList
            },
            success: function (res) {
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 1500,
                    success: function () {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getPersonalData()
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})